package com.frxuexi.repair.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.RepairBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.repair.R
import com.frxuexi.repair.RepairIntent
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.databinding.ActivityRepairDetailBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairDetailActivity")
@AndroidEntryPoint
class RepairDetailActivity : BaseActivity<ActivityRepairDetailBinding,RepairIntent,RepairViewModel>() {

    @Autowired
    lateinit var rid:String

    lateinit var item: RepairBeanItem

    override fun getVM(): RepairViewModel {
        return viewModels<RepairViewModel>().value
    }

    override fun getBind(): ActivityRepairDetailBinding {
        return ActivityRepairDetailBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                item = it.data as RepairBeanItem
                                binding.bean=item
                                if(item.processingStatus=="已完成"){
                                    binding.repDetailProCircle2.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_darkgreen)
                                    binding.repDetailProCircle3.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_darkgreen)
                                }else if(item.processingStatus=="处理中"){
                                    binding.repDetailProCircle2.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_darkgreen)
                                }

                                if(item.processingStatus!="已完成"){
                                    binding.repDetailGroup.visibility=View.VISIBLE
                                }
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("操作完成")
                                finish()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(RepairIntent.getRepairById(rid.toInt()))

        binding.repDetailOk.setOnClickListener {
            item.processingStatus="处理中"
            vm.sendIntent(RepairIntent.updateRepair(item.id!!,item))
        }

        binding.repDetailRefause.setOnClickListener {
            item.processingStatus="已完成"
            vm.sendIntent(RepairIntent.updateRepair(item.id!!,item))
        }
    }

}